package com.ssn.controller.admin;

import com.ssn.po.Blog;
import com.ssn.po.User;
import com.ssn.service.IBlogService;
import com.ssn.service.ITagService;
import com.ssn.service.ITypeService;
import com.ssn.vo.BlogQuery;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Resource
    private IBlogService blogService;
    @Resource
    private ITypeService typeService;
    @Resource
    private ITagService tagService;

    private static final String INPUT  = "admin/input-blog";
    private static final String LIST  = "admin/blogs";
    private static final String REDIRECT_LIST  = "redirect:/admin/blogs";


    @GetMapping("/blogs")
    public String blogPage(Model model, @PageableDefault(size = 4,sort = {"createTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        model.addAttribute("types",typeService.listType());
        return LIST;
    }

    @PostMapping("/blogs/search")
    public String search(Model model, @PageableDefault(size = 4,sort = {"createTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs :: blogList";
    }
    @GetMapping("/blogs/input")
    public String inputPage(Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog",new Blog());
        return INPUT;
    }

    @GetMapping("/blogs/{id}/input")
    public String editPage(Model model, @PathVariable Long id){
        Blog blog = blogService.findById(id);
        blog.initTags();
        model.addAttribute("blog",blog);
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        return INPUT;
    }
    @PostMapping("/blogs")
    public String post(Blog blog, HttpSession session, RedirectAttributes attributes){
        blog.setUser((User)session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog b;
        if (blog.getId() == null){
            b = blogService.saveBlog(blog);
        }else {
            b = blogService.updateBlog(blog.getId(),blog);
        }
        if (b == null){
            attributes.addFlashAttribute("message","操作失败");
        }else{
            attributes.addFlashAttribute("message","操作成功");
        }



        return REDIRECT_LIST;
    }


    @GetMapping("/blogs/{id}/delete")
    public String deleteType(@PathVariable Long id){
        blogService.deleteBlog(id);
        return REDIRECT_LIST;
    }







}
